package cn.com.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.service.ICepositService;
import com.cabinet.service.ILatticeService;
import com.cabinet.web.entity.Ceposit;
import com.cabinet.web.entity.Lattice;

import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/ceposit")
public class CepositConroller {
	
	@Autowired
	private ILatticeService iLatticeService;
	
	@Autowired
	private ICepositService iCepositService;
	
	public String index(){
		return "/ceposit/ceposit";
	}
	
	@RequestMapping("/open")
	@ResponseBody
	public String open(Long lid,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		
		Lattice la = iLatticeService.getLattice(lid);
		if(la.getlState() == 1){
			String userid = CookieUtils.getCookieValue(request, "userid");
			if(userid == "null" || userid == null){
				//System.out.println("未登录");
				CookieUtils.setCookieRedirectUrl(request, response, "/commodities");
				return "1";//用户未登录
			}else{
				la.setlState(2);
				iLatticeService.updateLattice(la);
				Ceposit c = new Ceposit();
				c.setCabId(la.getCabId());
				c.setIsPick(1);
				c.setLatticeID(la.getId());
				c.setUserId(Long.valueOf(userid));
				iCepositService.addCeposit(c);
				return "柜门正在打开";
			}
		}else{
			return "该格子已被使用";
		}
	}
	
	@RequestMapping("/myceposit")
	public String getMyCeposit(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println("未登录");
			CookieUtils.setCookieRedirectUrl(request, response, "/ceposit/myceposit");
			return "/login";//用户未登录
		}else{
			List<Ceposit> c = iCepositService.getCepositList(Long.valueOf(userid));
			modelMap.addAttribute("Ceposit", c);
			return "/user/user_ceposit_order";
		}	
	}
	
	@RequestMapping("/pick")
	public String pickCeposit(Long id,HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws Exception{
		System.out.println(id);
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println("未登录");
			CookieUtils.setCookieRedirectUrl(request, response, "/ceposit/myceposit");
			return "redirect:/login";//用户未登录
		}else{
			Ceposit c = iCepositService.getCeposit(id);
			if(c.getUserId().toString().equals(userid)){
				System.out.println(c.getUserId());
				c.setIsPick(2);
				iCepositService.addCeposit(c);
				Lattice la = iLatticeService.getLattice(c.getLatticeID());
				la.setlState(1);
				iLatticeService.updateLattice(la);
				
			}
			//List<Ceposit> c = iCepositService.getCepositList(Long.valueOf(userid));
			//modelMap.addAttribute("Ceposit", c);
			return "redirect:/ceposit/myceposit";
		}		
		
	}

}
